<?php
namespace src\lib;

use src\Curl;

/**
 * 百度图床
 * Class Baidu
 * @package src\lib
 * @url https://baijiahao.baidu.com/
 */

class Baidu
{
    public function upload($path)
    {
        $data = [
            'media' => Curl::getCurlFile($path),
            'no_compress' => '1',
            'id' => 'WU_FILE_0',
            'is_avatar' => '0',
            'type' => 'image',
            'name' => pathinfo($path, PATHINFO_FILENAME) . '.jpg'
        ];
        $result = Curl::post('https://baijiahao.baidu.com/builderinner/api/content/file/upload', $data, [
            CURLOPT_HTTPHEADER => [
                'Origin: https://baijiahao.baidu.com',
                'Referer: https://baijiahao.baidu.com/builder/app/register?type=individual'
            ],
        ]);

        $json = json_decode($result, true);
        if(isset($json['errno']) && $json['errno'] == 0 && isset($json['ret']['org_url'])){
            return $json['ret']['org_url'];
        }

        return false;
    }

}